var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
var util = require("../../utils/util.js");
var FLAG = true;
var load = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customer_code:'',
    Equipment:[],
    empty:true,
    hasMore:false,
    loading:false,
    empty_text:"未搜索到缴费器哟"

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      customer_code: options.customer_code,
      Equipment:[]
    });
    that.searchEquipment();
  },
  //搜索结果
  searchEquipment:function(){
    var that = this;
    app.ajax({
      url: 'User/Equipmentlists/searchEquipment',
      method: "POST",
      load: load,
      msg:"正在搜索缴费器~~",
      data: { customer_code: that.data.customer_code, page:_PAGE,pagesize:_PAGESIZE },
      success: function (res) {
        if (res.data.code == 1000) {
          //处理数据
          if(_PAGE==1){
            that.setData({
              Equipment: res.data.data,
              empty: false,
            });
          }else{
            that.setData({
              Equipment: that.data.Equipment.concat(res.data.data),
              empty: false
            });
          }
          //是否需要加载更多
          var hasMore = true;
          if(res.data.data.length<_PAGESIZE){
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading:false
          })
          load=false

        } else {
          that.setData({
            empty:true,
            empty_text:res.data.message
          })
        }

      }
    })

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if(!that.data.hasMore){
      return false;
    }
    _PAGE++;
    that.setData({
      loading:true
    })
    that.searchEquipment();
  },

  /**
   *
   * 绑定缴费器
   */
  bindFacility: function (e) {
    if(!FLAG){
      return false;
    }
    FLAG = false;
    var customer_code = e.currentTarget.dataset.customer_code;
    app.ajax({
      url: 'User/Equipmentlists/addEquipment',
      data: { customer_code: customer_code},
      success: function (res) {
        FLAG = true;
        if (res.data.code == 1000) {
          app.showToast(res.data.message);
          wx.switchTab({
            url: '../home/home',
          })
          return false;
        } else {
          app.showToast(res.data.message);
          return false;
        }
      }
    })


    return false;
    wx.navigateTo({
      url: '../facilityDetail/facilityDetail',
    })
  },
  //继续绑定缴费器
  continueBind:function(){
    wx.redirectTo({
      url: '../addEquipment/addEquipment',
    })
  },
  //跳转缴费器详情
  facilityDetail(e) {
    var that = this;
    var is_bind = e.currentTarget.dataset.is_bind;
    var equipment_id = e.currentTarget.dataset.equipment_id;
    console.log(is_bind);
    if(!is_bind){
      return;
    }
    wx.navigateTo({
      url: '../facilityDetail/facilityDetail?equipment_id=' + equipment_id,
    })
  },
  //去商城
  goShop() {
    wx.switchTab({
      url: '../shopping/shoppingHome/shoppingHome'
    })
  }
})
